import { Component, OnInit } from '@angular/core';
import {TipService} from "../../services/tip.service";
import Common from '../../common/common';
import {ExtendHttpService} from "../../services/extend-http.service";
import {Router} from "@angular/router";
import {SocketService} from "../../services/socket.service";

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  login = {mobile: '',login_pwd: ''};
  isLoading = false;
  bottomMarginTop:number = 0;
  constructor(
    private tipService: TipService,
    private extendHttp: ExtendHttpService,
    private router: Router,
    private socketService: SocketService
  ) { }

  ngOnInit() {
    let w1 = 159 + 238 + 53 + 28;
    let w2 = $(document).height();
    this.bottomMarginTop = w2 - w1;
  }

  // 提交登录
  submitLogin() {
    if (!this.login.mobile.trim()) {
      this.tipService.change(true, '请输入手机号');
      return;
    }
    if (!Common.mobileReg(this.login.mobile)) {
      this.tipService.change(true, '手机号格式不正确');
      return;
    }

    if (!this.login.login_pwd.trim()) {
      this.tipService.change(true, '请输入密码');
      return;
    }

    // 密码md5加密
    let params2 = Object.assign({}, this.login);
    params2.login_pwd = hex_md5(params2.login_pwd);

    this.isLoading = !this.isLoading;
    this.extendHttp.post('/agentApp/login', params2).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        localStorage.setItem('loginInfo',JSON.stringify(res));
        this.router.navigateByUrl('/main/index');
      } else {
        this.tipService.change(true, res['message']);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
  }

}
